package com.controller;

import javax.xml.ws.RequestWrapper;
import java.io.File;

@Controller
public class FileUploadController {
    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file, HttpServletRequest request) {
        // 获取上传文件真实目录 获取项目所在目录
        String path = request.getServletContext().getRealPath("/");
        // 上传目录
        String uploadPath = "/static/upload";
        // 上传文件名
        String fileName = file.getOriginalFilename();
        String filePath = path + uploadPath + fileName;
        if (!file.isEmpty()) {
            file.transferTo(new File(filePath));
            return "uploadSuccess";//显示成功页面
        }
        return "uploadFailure";//WEB-INF/pages/uploadFailure.jsp
    }

    @RequestMapping("/uploads")
    public String upload(MultipartFile[] files, HttpServletRequest request) {
        String path = request.getServletContext().getRealPath("/");
        String uploadPath = "/static/upload";
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            String filePath = path + uploadPath + fileName;
            if (!file.isEmpty()) {
                file.transferTo(new File(filePath));
            }
        }
        return "uploadSuccess";
    }
}